﻿using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Macds.Core.Remoting
{
    public class RemoteFactory
    {
        private BindingType type;

        public RemoteFactory(BindingType type)
        {
            this.type = type;
        }

        public TContract CreateInstance<TContract>(ChannelFactory<TContract> channel, string uri, string domainName)
        {
            ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(TContract)),
                    BindingFactory.CreateBinding(type),
                    new EndpointAddress(new Uri(uri), EndpointIdentity.CreateDnsIdentity(domainName)));

            channel = new ChannelFactory<TContract>(endpoint);
            return channel.CreateChannel();
        }
    }
}
